'use strict';
//店员列表
import React,{Component} from 'react';
import Header from '../../common/header';
import * as URLconstant from '../../constant/URLconstant'
import Util from '../../common/util';
import Toast from '../../common/ToastUtil';
import DesFans from './desFans';
import {
    View,
    Text,
    TouchableOpacity,
    Image,
    StyleSheet,
    ScrollView,
    Alert
} from 'react-native';
export default class clerkList extends Component{
    // 构造
      constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            clerkList:[],
            shopOwner:[],
            loaded:true
        };
          this.reFresh=this.reFresh.bind(this)
      }

    componentDidMount() {
        this.getClerk()
    }

      getClerk(){
          let url = URLconstant.GET_CLERK_LIST
          let datakey=[]
          datakey['user_id']=this.props.user_id
          datakey['shop_id']=this.props.shop_id
          Util.get(url,datakey,this.getClerkSuc.bind(this),this.errorCallback.bind(this))
      }

    getClerkSuc(datas){
        if(datas.status==0){
            this.setState({
                clerkList:datas.data.member_list.dianyuan,
                shopOwner:datas.data.member_list.dianzhu,
                loaded:false
            })
        }
    }

    errorCallback(){

    }

    deleteUser(user_id,fansCount,shop_member_id,shop_id){
        if(fansCount>0){
            Alert.alert(
                '删除提醒',
                '该店员有粉丝团队，删除前需要分配粉丝',
                [
                    {text:'前往分配',onPress:()=>this.toDesFans(user_id)},
                    {text:'取消'}
                ]
            )
        }else{
            Alert.alert(
                '删除提醒',
                "是否确定删除",
                [
                    {text:'确认删除',onPress:()=>this.sureDel(shop_member_id,shop_id)},
                    {text:'取消'}
                ]
            )
        }
    }

    toDesFans(user_id){
          this.props.navigator.push({
              component:DesFans,
              passProps:{
                  del_fans:user_id,
                  reFresh:this.reFresh,
                  user_id:this.props.user_id
              }
          })
    }

    reFresh(){
          this.getClerk()
    }

    sureDel(shop_member_id,shop_id){
          let url = URLconstant.DELETE_CLERK
        let datakey=[]
        datakey['user_id'] = this.props.user_id
        datakey['shop_id'] = shop_id
        datakey['shop_member_id'] = shop_member_id
        Util.post(url,datakey,this.delClerkSuc.bind(this),this.errorCallback.bind(this))
    }

    delClerkSuc(datas){
          if(datas.status==0){
              this.getClerk()
          }
          Toast.show(datas.msg)
    }

      render(){
          return(
              <View style={{flex:1,backgroundColor:"#f0f0f0"}}>
                  <Header navigator={this.props.navigator} initObj={{ title: '店员列表',id:2}}/>
                  {
                      this.state.loaded?
                          <View style={{flex:1}}>
                              {Util.loading()}
                          </View>:
                          <ScrollView style={{flex:1}}>
                              <View style={styles.member_view}>
                                <View style={styles.header_view}>
                                    <Image
                                        source={require('../../imgs/dianzhu.png')}
                                        style={{width:25,height:25,marginRight:5}}
                                        resizeMode={'cover'}
                                    />
                                    <Text style={{fontSize:14,color:"#333"}}>店主</Text>
                                </View>
                                  {
                                      this.state.shopOwner.map((item,index)=>{
                                          return(
                                              <View key={index} style={styles.member_item}>
                                                  <View style={styles.item_flex}>
                                                      <Image
                                                          source={{uri:item.headimage}}
                                                          style={{width:40,height:40,borderRadius:20}}
                                                          resizeMode={'cover'}
                                                      />
                                                      <View style={styles.member_info_view}>
                                                          <View style={styles.txt_view}>
                                                              <ScrollView horizontal={true} style={{flex:1}}>
                                                                  <Text style={{fontSize:13,color:"#333"}}>{item.real_name}({item.user_id})</Text>
                                                              </ScrollView>
                                                          </View>
                                                          <View style={styles.txt_view}>
                                                              <Text style={{fontSize:13,color:"#333"}}>团队数:</Text>
                                                              <ScrollView horizontal={true} style={{flex:1}}>
                                                                  <Text style={{fontSize:13,color:"#cc1d22"}}>{item.fansCount}</Text>
                                                              </ScrollView>
                                                          </View>
                                                          <View style={styles.txt_view}>
                                                              <Text style={{fontSize:13,color:"#333"}}>归属门店:</Text>
                                                              <ScrollView horizontal={true} style={{flex:1}}>
                                                                  <Text style={{fontSize:13,color:"#cc1d22"}}>{item.shop_name}</Text>
                                                              </ScrollView>
                                                          </View>
                                                      </View>
                                                  </View>
                                                  <View style={styles.item_flex}>
                                                      <View style={styles.member_info_view}>
                                                          <View style={styles.txt_view2}>
                                                              <Text style={{fontSize:13,color:"#333"}}>今日业绩:</Text>
                                                              <Text style={{fontSize:13,color:"#cc1d22"}}>¥{item.current_day_sale||0}</Text>
                                                          </View>
                                                          <View style={styles.txt_view2}>
                                                              <Text style={{fontSize:13,color:"#333"}}>月总业绩:</Text>
                                                              <Text style={{fontSize:13,color:"#cc1d22"}}>¥{item.current_month_sale||0}</Text>
                                                          </View>
                                                          <View style={styles.txt_view2}>
                                                              <Text style={{fontSize:13,color:"#333"}}>年总业绩:</Text>
                                                              <Text style={{fontSize:13,color:"#cc1d22"}}>¥{item.current_year_sale||0}</Text>
                                                          </View>
                                                      </View>
                                                      <TouchableOpacity onPress={()=>this.deleteUser(item.user_id,item.fansCount,item.id,item.shop_id)}>
                                                          <Image
                                                              source={require('../../imgs/alter_delete.png')}
                                                              style={{width:25,height:25}}
                                                              resizeMode={'cover'}
                                                          />
                                                      </TouchableOpacity>
                                                  </View>
                                              </View>
                                          )
                                      })
                                  }
                              </View>
                              <View style={styles.member_view}>
                                  <View style={styles.header_view}>
                                      <Image
                                          source={require('../../imgs/yuangong.png')}
                                          style={{width:25,height:25,marginRight:5}}
                                          resizeMode={'cover'}
                                      />
                                      <Text style={{fontSize:14,color:"#333"}}>店员</Text>
                                  </View>
                                  {
                                      this.state.clerkList.map((item,index)=>{
                                          return(
                                              <View key={index} style={styles.member_item}>
                                                  <View style={styles.item_flex}>
                                                      <Image
                                                          source={{uri:item.headimage}}
                                                          style={{width:40,height:40,borderRadius:20}}
                                                          resizeMode={'cover'}
                                                      />
                                                      <View style={styles.member_info_view}>
                                                          <View style={styles.txt_view}>
                                                              <ScrollView horizontal={true} style={{flex:1}}>
                                                                  <Text style={{fontSize:13,color:"#333"}}>{item.real_name}({item.user_id})</Text>
                                                              </ScrollView>
                                                          </View>
                                                          <View style={styles.txt_view}>
                                                              <Text style={{fontSize:13,color:"#333"}}>团队数:</Text>
                                                              <ScrollView horizontal={true} style={{flex:1}}>
                                                                  <Text style={{fontSize:13,color:"#cc1d22"}}>{item.fansCount}</Text>
                                                              </ScrollView>
                                                          </View>
                                                          <View style={styles.txt_view}>
                                                              <Text style={{fontSize:13,color:"#333"}}>归属门店:</Text>
                                                              <ScrollView horizontal={true} style={{flex:1}}>
                                                                  <Text style={{fontSize:13,color:"#cc1d22"}}>{item.shop_name}</Text>
                                                              </ScrollView>
                                                          </View>
                                                      </View>
                                                  </View>
                                                  <View style={styles.item_flex}>
                                                      <View style={styles.member_info_view}>
                                                          <View style={styles.txt_view2}>
                                                              <Text style={{fontSize:13,color:"#333"}}>今日业绩:</Text>
                                                              <Text style={{fontSize:13,color:"#cc1d22"}}>¥{item.current_day_sale||0}</Text>
                                                          </View>
                                                          <View style={styles.txt_view2}>
                                                              <Text style={{fontSize:13,color:"#333"}}>月总业绩:</Text>
                                                              <Text style={{fontSize:13,color:"#cc1d22"}}>¥{item.current_month_sale||0}</Text>
                                                          </View>
                                                          <View style={styles.txt_view2}>
                                                              <Text style={{fontSize:13,color:"#333"}}>年总业绩:</Text>
                                                              <Text style={{fontSize:13,color:"#cc1d22"}}>¥{item.current_year_sale||0}</Text>
                                                          </View>
                                                      </View>
                                                      <TouchableOpacity onPress={()=>this.deleteUser(item.user_id,item.fansCount,item.id,item.shop_id)}>
                                                          <Image
                                                              source={require('../../imgs/alter_delete.png')}
                                                              style={{width:25,height:25}}
                                                              resizeMode={'cover'}
                                                          />
                                                      </TouchableOpacity>
                                                  </View>
                                              </View>
                                          )
                                      })
                                  }
                              </View>
                          </ScrollView>
                  }
              </View>
          )
      }
}
var styles = StyleSheet.create({
    member_view:{
        backgroundColor:"#fff",
        marginBottom:10
    },
    header_view:{
        height:35,
        flexDirection:'row',
        alignItems:'center',
        paddingHorizontal:10
    },
    member_item:{
      flexDirection:'row',
        padding:10,
    },
    item_flex:{
      flexDirection:'row',
      flex:1,
        alignItems:'center'
    },
    member_info_view:{
        height:60,
        flex:1,
        justifyContent:'space-between',
        paddingHorizontal:10
    },
    txt_view:{
        flex:1,
        flexDirection:'row'
    },
    txt_view2:{
        flex:1,
        flexDirection:'row',
        justifyContent:'space-between'
    }
})